//引入SDK核心类
const QQMapWX = require("../components/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min");

//实例化API核心类
const qqmapsdk = new QQMapWX({
    key: 'TAWBZ-YSA3S-LV6O6-6PR5A-SYPXQ-OOF6F'
});

//获取当前地址（经纬度转具体地址）
export const getAddress = () => {
    return new Promise((resolve, reject) => {
        wx.getLocation({
            type: 'gcj02',
            altitude: 'altitude',
            isHighAccuracy: true,
            success: res => {
                const { latitude, longitude } = res;
                //使用腾讯地图api进行逆地址解析 
                qqmapsdk.reverseGeocoder({
                    location: {
                        latitude: latitude,
                        longitude: longitude
                    },
                    success: result => {
                        const { address, address_component, location } = result.result;
                        address_component.lat = location.lat;
                        address_component.lng = location.lng;
                        resolve(result);
                    }
                })
            },
            fail: err => {
                wx.showModal({
                    title: '位置授权提示',
                    content: '您已关闭此小程序获取位置信息权限，请先打开小程序获取位置信息权限进行登录',
                    showCancel: false,
                    complete: (res) => {
                        console.log(res, 'res');
                    }
                })
            }
        });
    })
};

//地址逆解析（经纬度转地址）
export const getlatlngAddress = (obj = {}) => {
    return new Promise((resolve, reject) => {
        qqmapsdk.reverseGeocoder({
            location: {
                latitude: obj.latitude,
                longitude: obj.longitude,
            },
            success: result => {
                const { address, address_component, location, formatted_addresses } = result.result;
                address_component.lat = location.lat;
                address_component.lng = location.lng;
                resolve(formatted_addresses);
            }
        })
    })
};

//地址逆解析（具体地址转经纬度）
export const getLnglat = (val) => {
    return new Promise((resolve, reject) => {
        qqmapsdk.geocoder({
            address: val,
            success: res => {
                console.log(res, 'resss');
                let { message, result, status } = res;
                if (message === 'query ok' && status === 0) {
                    resolve(result);
                } else {
                    wx.showToast({
                        icon: 'none',
                        title: '地址解析失败',
                    })
                }
            },
            fail: err => {
                wx.showToast({
                    icon: "none",
                    title: err.message,
                });
            }
        });
    });
};

//根据关键持搜素地址
export const getKeyAddress = (val) => {
    return new Promise((resolve, reject) => {
        qqmapsdk.getSuggestion({
            keyword: val, //用户输入的关键词，可设置固定值,如keyword:'KFC'
            // region: '金华市', //设置城市名，限制关键词所示的地域范围，非必填参数
            // region_fix: 0,
            // sig: getApp().globalData.sig,
            success: function (res) { //搜索成功后的回调
                if (res.message === 'query ok' && res.status === 0) {
                    resolve(res.data);
                } else {
                    wx.showToast({
                        icon: 'none',
                        title: '地址搜索失败',
                    })
                };
            },
            fail: function (err) {
                wx.showToast({
                    icon: "none",
                    title: err.message,
                });
            },
        });
    });
};

//根据经纬度计算出具体距离
export const getMapDistance = (lng1, lat1, lng2, lat2) => {
    var d1 = 0.01745329251994329;
    var d2 = lng1;
    var d3 = lat1;
    var d4 = lng2;
    var d5 = lat2;
    d2 *= d1;
    d3 *= d1;
    d4 *= d1;
    d5 *= d1;
    var d6 = Math.sin(d2);
    var d7 = Math.sin(d3);
    var d8 = Math.cos(d2);
    var d9 = Math.cos(d3);
    var d10 = Math.sin(d4);
    var d11 = Math.sin(d5);
    var d12 = Math.cos(d4);
    var d13 = Math.cos(d5);
    var arrayOfDouble1 = [];
    var arrayOfDouble2 = [];
    arrayOfDouble1.push(d9 * d8);
    arrayOfDouble1.push(d9 * d6);
    arrayOfDouble1.push(d7);
    arrayOfDouble2.push(d13 * d12);
    arrayOfDouble2.push(d13 * d10);
    arrayOfDouble2.push(d11);
    var d14 = Math.sqrt((arrayOfDouble1[0] - arrayOfDouble2[0]) * (arrayOfDouble1[0] - arrayOfDouble2[0]) +
        (arrayOfDouble1[1] - arrayOfDouble2[1]) * (arrayOfDouble1[1] - arrayOfDouble2[1]) +
        (arrayOfDouble1[2] - arrayOfDouble2[2]) * (arrayOfDouble1[2] - arrayOfDouble2[2]));
    var distance = (Math.asin(d14 / 2.0) * 12742001.579854401);
    if (distance > 1000) {
        distance = (distance / 1000).toFixed(2) + 'km'
    } else {
        distance = distance.toFixed(0) + 'm'
    }

    return distance;
}

//选择地图类型
// export const selectAddressType = () => {
   
// };